package com.vcredit.creditcat.web.app;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import com.alibaba.dubbo.common.utils.IOUtils;
import com.fasterxml.jackson.core.JsonEncoding;
import com.vcredit.creditcat.exception.BasicInfoException;
import com.vcredit.creditcat.util.JsonUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vcredit.creditcat.api.IAuthPersonalInfoManager;
import com.vcredit.creditcat.dto.authpersonalinfo.AuthPersonalInfoDto;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.vo.JsonResult;
import com.vcredit.framework.annotation.Log;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Map;

@RestController
@RequestMapping(value = "/basicInfo")
public class BasicInfoController extends BaseCreditCatController {

	@Reference(version = "1.0")
	private IAuthPersonalInfoManager authPersonalInfoManager;
	private static Logger logger = LoggerFactory.getLogger(BasicInfoController.class);

	/**
	 * 保存用户个人基本信息（学历、月收入、婚姻等等）
	 * @author zhaoqi01
	 * @param param
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	@Log
	@RequestMapping(value = "/saveInfo")
	public JsonResult saveInfo(@RequestBody @Valid AuthPersonalInfoDto param, HttpServletRequest request) throws Exception {
		Cust cust = getCust(request);
		authPersonalInfoManager.insert(param,cust);
		return JsonResult.SuccessResponse(null);
	}

	/**
	 * 获取个人基本信息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Log
	@RequestMapping(value = "/getPersonalInfo")
	public JsonResult getPersonalInfo(@RequestBody @Valid Map<String,String> map,HttpServletRequest request)throws Exception{
		Cust cust = getCust(request);
		String authCode=map.get("authCode");
		Map<String,Object> result= authPersonalInfoManager.getPersonalInfo(cust,authCode);
		return JsonResult.SuccessResponse(result);
	}
	@Log
	@RequestMapping(value = "/savePersonalInfo")
	public JsonResult savePersonalInfo(Map<String,Object> map,HttpServletRequest request)throws Exception{
		Cust cust = getCust(request);
		BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream(), "utf-8"));
		String body = IOUtils.read(reader);
//		Object basicInfo=map.get("basicInfo");
//		Object productIdObj=map.get("productId");
//		logger.info(cust.getCustId()+"-----"+JacksonUtil.Obj2Json(map));
//		if(basicInfo!=null && productIdObj!=null && StringUtils.isNotBlank(productIdObj.toString())){
//			Long productId=Long.valueOf(productIdObj.toString());
//			Map<String,Object> param=(Map<String,Object>)basicInfo;
//			if(param.containsKey("personalInfoId")){
//				authPersonalInfoManager.savePersonalInfo(productId,cust,param);
//			}else{
//				if(StringUtils.isBlank(body)){
//					throw new BasicInfoException("基本信息保存失败");
//				}
//				Map<String, Object> dataMap = JacksonUtil.Json2Map(body);
//				if(!dataMap.containsKey("basicInfo")|| !dataMap.containsKey("productId")){
//					throw new BasicInfoException("基本信息保存失败");
//				}
//				param=(Map<String,Object>)dataMap.get("basicInfo");
//				if(param.containsKey("personalInfoId")){
//					authPersonalInfoManager.savePersonalInfo(productId,cust,param);
//				}else{
//					throw new BasicInfoException("基本信息保存失败");
//				}
//			}
//		}else{
			if(StringUtils.isBlank(body)){
				throw new BasicInfoException("基本信息提交失败");
			}
		   logger.info(body);
			Map<String, Object> dataMap = (Map<String, Object>)JsonUtil.json2Map(body);
			if(!dataMap.containsKey("basicInfo")|| !dataMap.containsKey("productId")){
				throw new BasicInfoException("基本信息提交失败");
			}
			Map<String,Object> param=(Map<String,Object>)dataMap.get("basicInfo");
			Long productId=Long.valueOf(dataMap.get("productId").toString());
			if(param.containsKey("personalInfoId")){
				authPersonalInfoManager.savePersonalInfo(productId,cust,param);
			}else{
				throw new BasicInfoException("基本信息提交失败");
			}
		return JsonResult.SuccessResponse(null);
	}
}
